//
//  HFVisitorView.swift
//  HFWeChat
//
//  Created by 黄来峰 on 16/4/18.
//  Copyright © 2016年 itcast. All rights reserved.
//


import UIKit
import SnapKit

class HFVisitorView: UIView {
    //定义一个B包
    var registerAndLoginButonClick :(()->())?
    
    

   override  init(frame: CGRect) {
    super.init(frame: frame)
       setUpUI()
    
    
    }
    

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
     //MARK -设置视图
    private func setUpUI(){
        //设置背景颜色
        backgroundColor = RGB(red: 237, green: 237, blue: 237)

//    backgroundColor = ThemeColor
        //设置随机色
//        backgroundColor = RandomColor()
        //添加控件
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //为控件设置约束
        circleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(16)
            make.centerX.equalTo(self)
            make.height.equalTo(36)
            make.width.equalTo(230)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
            
        }
    }
    //MARK - 让圆圈转起来
    func circleRun(){
        //定义一个基本动画
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画执行时间
        ani.duration = 15
        ani.repeatCount = MAXFLOAT
        ani.toValue = 2*M_PI
        
        
        //默认当我们将程退到后台或则切换tabbar的时候默认动画移除，使用下面的方法可以让动画不被移除
        ani.removedOnCompletion = false
        
        circleImageView.layer.addAnimation(ani, forKey: nil)
        
    }
    
    
    
     //MARK - 共外界调用，用来更换访客视图中的图片
    func setVistorInfo(imageName: String?,titleMessage: String?){
        if let img = imageName,title = titleMessage{
        
            iconImageView.image = UIImage(named: img)
            messageLabel.text = title
            circleImageView.hidden = true
        
        }else{
        
            //代表首页
            
            circleRun()
            
        }
        
        
    }
    
    
     //MARK - 懒加载控件--以B包的形式进行加载
   private lazy var circleImageView:UIImageView = {
    let cirImage = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    cirImage.sizeToFit()
    
    return cirImage
    }()
    
     //MARK - 懒加载遮罩--懒加载遮罩-直接进行加载，而且不用写上初始化符号
    private lazy var maskImageView: UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_mask_smallicon"))
     //MARK - 懒加载小房子 --以B包的形式进行加载
    lazy var iconImageView: UIImageView = {
        let icon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return icon
        
    }()
    
     //MARK - 懒加载消息控件
   private lazy var messageLabel: UILabel = {
    
        let lab = UILabel()
        lab.textColor = UIColor.grayColor()
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lab.textAlignment = .Center
        lab.numberOfLines = 0
        lab.font = UIFont.systemFontOfSize(14)
        return lab
    }()
     //MARK - 懒加载注册控件
    private lazy var registerButton:UIButton = {
    
        let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.addTarget(self, action: "registerOrLoginButonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.backgroundColor = UIColor.whiteColor() 
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        return button
    }()
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "registerOrLoginButonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.backgroundColor = UIColor.whiteColor()
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        
        return button
    }()
    
    
   @objc private func registerOrLoginButonClick(){
    
    registerAndLoginButonClick?()

    print("登录")
    }

}
